const Service = require('egg').Service;

class newsService extends Service {
  async find(uid) {
      // 引入数据库
      const client1 = this.app.mysql.get('db1');
      // sql查询
      const news = await client1.query('select * from news where id = ?', uid);
      // CRUD 语句
        // 单条查询
      const news = await client1.get('news', { id: uid });
        // 多条查询
      const news = await client1.select('news');
        // 条件查询和结果定制
      const news = await this.app.mysql.select('news', { // 搜索 news 表
        where: { status: 'draft', author: ['author1', 'author2'] }, // WHERE 条件
        columns: ['author', 'title'], // 要查询的表字段
        orders: [['created_at','desc'], ['id','desc']], // 排序方式
        limit: 10, // 返回数据量
        offset: 0, // 数据偏移量
      });

      // 引入第二个库，操作如上
      const client2 = this.app.mysql.get('db2');
      const news2 = await client2.query('select * from test2table where id = ?', uid);
  
    return news
  }
}

module.exports = newsService;